package com.chen.palmar.common.datasource;


import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Environment;

import com.chen.palmar.common.constant.AppConstant;
import com.chen.palmar.common.utils.DES3;
import com.chen.palmar.entity.AgencyDetailEntity;
import com.chen.palmar.entity.AgencyListEntity;
import com.chen.palmar.entity.AgencyTagEntity;
import com.chen.palmar.entity.AliPayEntity;
import com.chen.palmar.entity.ApplyAgencyRequest;
import com.chen.palmar.entity.ApplyInfoEntity;
import com.chen.palmar.entity.ApplyInfoRequest;
import com.chen.palmar.entity.ApplyManagerEntity;
import com.chen.palmar.entity.ApplyStoreRequest;
import com.chen.palmar.entity.ApplyTagEntity;
import com.chen.palmar.entity.AuthEntity;
import com.chen.palmar.entity.CityListEntity;
import com.chen.palmar.entity.CollectEntity;
import com.chen.palmar.entity.CollectionAgencyEntity;
import com.chen.palmar.entity.CollectionInfoEntity;
import com.chen.palmar.entity.CollectionProducerEntity;
import com.chen.palmar.entity.CollectionStoreEntity;
import com.chen.palmar.entity.DetailEntity;
import com.chen.palmar.entity.EditShopEntity;
import com.chen.palmar.entity.GoodEntity;
import com.chen.palmar.entity.HomeBanEntity;
import com.chen.palmar.entity.HomeInfoEntity;
import com.chen.palmar.entity.HomeListEntity;
import com.chen.palmar.entity.LoginEntity;
import com.chen.palmar.entity.MessageCallEntity;
import com.chen.palmar.entity.MessageCommentEntity;
import com.chen.palmar.entity.MessageNoticeEntity;
import com.chen.palmar.entity.NewListEntity;
import com.chen.palmar.entity.PayWXEntity;
import com.chen.palmar.entity.ProducerBaseEntity;
import com.chen.palmar.entity.ProducerHouseEntity;
import com.chen.palmar.entity.ProducerInfoEntity;
import com.chen.palmar.entity.ProducerStoreEntity;
import com.chen.palmar.entity.SearchEntity;
import com.chen.palmar.entity.SelectProducerEntity;
import com.chen.palmar.entity.SetShopEntity;
import com.chen.palmar.entity.ShareEntity;
import com.chen.palmar.entity.ShopInfoEntity;
import com.chen.palmar.entity.ShopInfoRequest;
import com.chen.palmar.entity.ShopListEntity;
import com.chen.palmar.entity.StoreDetailEntity;
import com.chen.palmar.entity.TagListEntity;
import com.chen.palmar.entity.TypeListEntity;
import com.chen.palmar.entity.UserInfoEntity;
import com.chen.palmar.entity.VipEntity;
import com.orhanobut.logger.Logger;
import com.primb.androidlibs.net.config.ApiClient;
import com.primb.androidlibs.net.config.HttpConfig;
import com.primb.androidlibs.net.converter.RxSchedulerUtils;
import com.primb.androidlibs.net.entity.HttpResultEntity;
import com.primb.androidlibs.net.entity.WXEntity;
import com.primb.androidlibs.net.entity.WXPayEntity;
import com.primb.androidlibs.utils.FileIOUtils;
import com.primb.androidlibs.utils.FileUtils;
import com.primb.androidlibs.utils.FormatUtils;
import com.primb.androidlibs.utils.ImageUtils;
import com.primb.androidlibs.utils.JsonUtils;
import com.primb.androidlibs.utils.PhoneUtils;
import com.primb.androidlibs.utils.Utils;
import com.zaaach.citypicker.model.City;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import rx.Observable;
import rx.functions.Func1;


/**
 * @author Chen
 * @date 2017/11/15
 * 功能描述： 数据中心类
 */

public class DataCenter {
    private static ApiService apiService;
    public static HttpConfig httpConfig;
    public static final String CACHE_PATH = Environment.getExternalStorageDirectory().getPath() + AppConstant.LOAD_CACHE;

    public static void init() {
        apiService = ApiClient.initNetService(ApiService.class);
        httpConfig = HttpConfig.newInstance(Utils.getApp());
    }

    public static void configDeviceInfo() {
        httpConfig.setDeviceId(PhoneUtils.getIMEI());
        httpConfig.setDeviceModel(PhoneUtils.getSystemModel());
    }

    /**
     * 登录
     *
     * @param map
     * @return
     */
    public static Observable<LoginEntity> login(HashMap<String, String> map) {
        String params = configParams(map, false, false);
        return apiService.getLoginInfo(params).compose(RxSchedulerUtils.normalSchedulersTransformer());
    }

    /**
     * 第三方登录
     *
     * @param map
     * @return
     */
    public static Observable<LoginEntity> otherLoginInfo(HashMap<String, String> map) {
        String params = configParams(map, false, false);
        return apiService.otherLoginInfo(params).compose(RxSchedulerUtils.normalSchedulersTransformer());
    }

    /**
     * 获取微信信息
     *
     * @param map
     * @return
     */
    public static Observable<WXEntity> wxUserId(String url, HashMap<String, String> map) {
        return apiService.wxUserId(url, map).compose(RxSchedulerUtils.normalSchedulersTransformer());
    }

    /**
     * 微信支付
     *
     * @param map
     * @return
     */
    public static Observable<PayWXEntity> wxPay(HashMap<String, String> map) {
        String params = configParams(map, true, true);
        return apiService.wxPay(params).compose(RxSchedulerUtils.normalSchedulersTransformer());
    }

    /**
     * 支付宝支付
     *
     * @param map
     * @return
     */
    public static Observable<AliPayEntity> aliPay(HashMap<String, String> map) {
        String params = configParams(map, true, true);
        return apiService.aliPay(params).compose(RxSchedulerUtils.normalSchedulersTransformer());
    }

    /**
     * 微信支付测试接口
     *
     * @param map
     * @return
     */
    public static Observable<WXPayEntity> wxPay(String url, HashMap<String, String> map) {
        //        String params = configParams(map, true, true);
        return apiService.wxPayCeshi(url).compose(RxSchedulerUtils.normalSchedulersTransformer());
    }

    /**
     * 登出
     *
     * @return
     */
    public static Observable<HttpResultEntity> outLoginInfo() {
        HashMap<String, String> map = new HashMap<>();
        String params = configParams(map, false, true);
        return apiService.outLoginInfo(params).compose(RxSchedulerUtils.normalSchedulersTransformer());
    }

    /**
     * 获取验证码
     *
     * @param map
     * @return
     */
    public static Observable<AuthEntity> getAuthCode(HashMap<String, String> map) {
        String params = configParams(map, false, true);
        return apiService.getAuthCode(params).compose(RxSchedulerUtils.normalSchedulersTransformer());
    }

    /**
     * 验证码注册
     *
     * @param map
     * @return
     */
    public static Observable<LoginEntity> register(HashMap<String, String> map) {
        String params = configParams(map, false, false);
        return apiService.register(params).compose(RxSchedulerUtils.normalSchedulersTransformer());
    }

    /**
     * 忘记密码
     *
     * @param map
     * @return
     */
    public static Observable<HttpResultEntity> targetPwd(HashMap<String, String> map) {
        String params = configParams(map, true, true);
        return apiService.targetPwd(params).compose(RxSchedulerUtils.normalSchedulersTransformer());
    }

    /**
     * 第一次登陆设置
     *
     * @param map
     * @return
     */
    public static Observable<LoginEntity> firstSetting(HashMap<String, String> map) {
        String params = configParams(map, true, true);
        return apiService.firstSetting(params).compose(RxSchedulerUtils.normalSchedulersTransformer());
    }

    /**
     * 首页列表
     *
     * @param map
     * @return
     */
    public static Observable<HomeListEntity> homeListInfo(HashMap<String, String> map) {
        String params = configParams(map, true, true);
        return apiService.homeListInfo(params).compose(RxSchedulerUtils.normalSchedulersTransformer());
    }

    /**
     * 首页轮播图
     *
     * @param map
     * @retu
     */
    public static Observable<HomeBanEntity> homeBanInfo(HashMap<String, String> map) {
        String params = configParams(map, true, true);
        return apiService.homeBanInfo(params).compose(RxSchedulerUtils.normalSchedulersTransformer());
    }

    /**
     * 启动图
     *
     * @param map
     * @retu
     */
    public static Observable<HomeBanEntity> startImg(HashMap<String, String> map) {
        String params = configParams(map, false, false);
        return apiService.homeBanInfo(params).compose(RxSchedulerUtils.normalSchedulersTransformer());
    }

    /**
     * 首页资讯头条
     *
     * @param map
     * @return
     */
    public static Observable<HomeInfoEntity> homeInfoInfo(HashMap<String, String> map) {
        String params = configParams(map, true, true);
        return apiService.homeInfoInfo(params).compose(RxSchedulerUtils.normalSchedulersTransformer());
    }

    /**
     * 商品详情页
     *
     * @param map
     * @return
     */
    public static Observable<DetailEntity> detailInfo(HashMap<String, String> map) {
        String params = configParams(map, true, true);
        return apiService.detailInfo(params).compose(RxSchedulerUtils.normalSchedulersTransformer());
    }

    /**
     * 发表评论
     *
     * @param map
     * @return
     */
    public static Observable<HttpResultEntity> commentInfo(HashMap<String, String> map) {
        String params = configParams(map, true, true);
        return apiService.commentInfo(params).compose(RxSchedulerUtils.normalSchedulersTransformer());
    }

    /**
     * 点赞
     *
     * @param map
     * @return
     */
    public static Observable<GoodEntity> goodInfo(HashMap<String, String> map) {
        String params = configParams(map, true, true);
        return apiService.goodInfo(params).compose(RxSchedulerUtils.normalSchedulersTransformer());
    }

    /**
     * 收藏
     *
     * @param map
     * @return
     */
    public static Observable<CollectEntity> collectInfo(HashMap<String, String> map) {
        String params = configParams(map, true, true);
        return apiService.collectInfo(params).compose(RxSchedulerUtils.normalSchedulersTransformer());
    }

    /**
     * 取消收藏
     *
     * @param map
     * @return
     */
    public static Observable<CollectEntity> collectCancelInfo(HashMap<String, String> map) {
        String params = configParams(map, true, true);
        return apiService.collectCancelInfo(params).compose(RxSchedulerUtils.normalSchedulersTransformer());
    }

    /**
     * 经销商首页列表
     *
     * @param map
     * @return
     */
    public static Observable<AgencyListEntity> listAgencyInfo(HashMap<String, Object> map) {
        String params = configParams(map, true, true);
        return apiService.listAgencyInfo(params).compose(RxSchedulerUtils.normalSchedulersTransformer());
    }

    /**
     * 经销商城市列表
     *
     * @param map
     * @return
     */
    public static Observable<List<City>> listCityInfo(HashMap<String, Object> map) {
        String params = configParams(map, true, true);
        return apiService.listCityInfo(params).flatMap(new Func1<CityListEntity, Observable<List<City>>>() {
            @Override
            public Observable<List<City>> call(CityListEntity cityListEntity) {
                List<City> mlist = cityListEntity.getData();
                Collections.sort(mlist, new CityComparator());
                return Observable.just(mlist);
            }
        }).compose(RxSchedulerUtils.normalSchedulersTransformer());
    }

    /**
     * 经销商热门城市列表
     *
     * @param map
     * @return
     */
    public static Observable<List<City>> listHotCityInfo(HashMap<String, Object> map) {
        String params = configParams(map, true, true);
        return apiService.listHotCityInfo(params).flatMap(new Func1<CityListEntity, Observable<List<City>>>() {
            @Override
            public Observable<List<City>> call(CityListEntity cityListEntity) {
                List<City> mlist = cityListEntity.getData();
                return Observable.just(mlist);
            }
        }).compose(RxSchedulerUtils.normalSchedulersTransformer());
    }

    /**
     * 经销商详情
     *
     * @param map
     * @return
     */
    public static Observable<AgencyDetailEntity> agencyDetailInfo(HashMap<String, String> map) {
        String params = configParams(map, true, true);
        return apiService.agencyDetailInfo(params).compose(RxSchedulerUtils.normalSchedulersTransformer());
    }

    /**
     * 厂家首页new列表
     *
     * @param map
     * @return
     */
    public static Observable<NewListEntity> listProducerNew(HashMap<String, Object> map) {
        String params = configParams(map, true, true);
        return apiService.listProducerNew(params).compose(RxSchedulerUtils.normalSchedulersTransformer());
    }

    /**
     * 厂家首页shop列表
     *
     * @param map
     * @return
     */
    public static Observable<ShopListEntity> listProducerShop(HashMap<String, Object> map) {
        String params = configParams(map, true, true);
        return apiService.listProducerShop(params).compose(RxSchedulerUtils.normalSchedulersTransformer());
    }

    /**
     * 厂家首页type列表
     *
     * @param map
     * @return
     */
    public static Observable<TypeListEntity> listProducerType(HashMap<String, Object> map) {
        String params = configParams(map, true, true);
        return apiService.listProducerType(params).compose(RxSchedulerUtils.normalSchedulersTransformer());
    }

    /**
     * 厂家筛选列表
     *
     * @param map
     * @return
     */
    public static Observable<SelectProducerEntity> listProducerSelect(HashMap<String, Object> map) {
        String params = configParams(map, true, true);
        return apiService.listProducerSelect(params).compose(RxSchedulerUtils.normalSchedulersTransformer());
    }

    /**
     * 厂家详情
     *
     * @param map
     * @return
     */
    public static Observable<ProducerInfoEntity> listProducerInfo(HashMap<String, Object> map) {
        String params = configParams(map, true, true);
        return apiService.listProducerInfo(params).compose(RxSchedulerUtils.normalSchedulersTransformer());
    }

    /**
     * 厂家下的商品
     *
     * @param map
     * @return
     */
    public static Observable<ProducerStoreEntity> producerStoreInfo(HashMap<String, Object> map) {
        String params = configParams(map, true, true);
        return apiService.producerStoreInfo(params).compose(RxSchedulerUtils.normalSchedulersTransformer());
    }

    /**
     * 厂家下的企业信息
     *
     * @param map
     * @return
     */
    public static Observable<ProducerHouseEntity> producerHouseInfo(HashMap<String, Object> map) {
        String params = configParams(map, true, true);
        return apiService.producerHouseInfo(params).compose(RxSchedulerUtils.normalSchedulersTransformer());
    }

    /**
     * 厂家下的基本信息
     *
     * @param map
     * @return
     */
    public static Observable<ProducerBaseEntity> producerBaseInfo(HashMap<String, String> map) {
        String params = configParams(map, true, true);
        return apiService.producerBaseInfo(params).compose(RxSchedulerUtils.normalSchedulersTransformer());
    }

    /**
     * 商品详情
     *
     * @param map
     * @return
     */
    public static Observable<StoreDetailEntity> storeDetailInfo(HashMap<String, String> map) {
        String params = configParams(map, true, true);
        return apiService.storeDetailInfo(params).compose(RxSchedulerUtils.normalSchedulersTransformer());
    }

    /**
     * 商品询单
     *
     * @param map
     * @return
     */
    public static Observable<HttpResultEntity> contractSeller(HashMap<String, String> map) {
        String params = configParams(map, true, true);
        return apiService.contractSeller(params).compose(RxSchedulerUtils.normalSchedulersTransformer());
    }

    /**
     * 标签列表
     *
     * @return
     */
    public static Observable<TagListEntity> tagListInfo() {
        Map<String, String> map = new HashMap<>();
        String params = configParams(map, true, true);
        return apiService.tagListInfo(params).compose(RxSchedulerUtils.normalSchedulersTransformer());
    }

    /**
     * 发布信息标签列表
     *
     * @return
     */
    public static Observable<ApplyTagEntity> applyTagListInfo() {
        Map<String, String> map = new HashMap<>();
        String params = configParams(map, true, true);
        return apiService.applyTagListInfo(params).compose(RxSchedulerUtils.normalSchedulersTransformer());
    }

    /**
     * 发布商品
     *
     * @param map
     * @return
     */
    public static Observable<HttpResultEntity> applyStoreInfo(ApplyStoreRequest map) {
        ArrayList<MultipartBody.Part> parts = new ArrayList<>();
        for (int i = 0; i < map.getImgList().size(); i++) {
            if (map.getImgList().get(i) != null) {
                File file = new File(map.getImgList().get(i));
                if (file.exists()) {
                    RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
                    // Key，business_license
                    MultipartBody.Part body = MultipartBody.Part.createFormData("img" + (i + 1), file.getName(), requestBody);
                    parts.add(body);
                }
            }
        }
        String params = configParams(map, true, true);
        Observable<HttpResultEntity> info;
        if (map.getImgList().size() > 1) {
            info = apiService.applyStoreInfo(params, parts.toArray(new MultipartBody.Part[parts.size()]));
        } else {
            info = apiService.applyStoreInfo(params);
        }
        return info.compose(RxSchedulerUtils.normalSchedulersTransformer());
    }

    /**
     * 个人信息
     *
     * @return
     */
    public static Observable<UserInfoEntity> getUserInfo() {
        Map<String, String> map = new HashMap<>();
        String params = configParams(map, true, true);
        return apiService.getUserInfo(params).compose(RxSchedulerUtils.normalSchedulersTransformer());
    }

    /**
     * 修改昵称
     *
     * @return
     */
    public static Observable<HttpResultEntity> editUserName(HashMap<String, String> map) {
        String params = configParams(map, true, true);
        return apiService.editUserName(params).compose(RxSchedulerUtils.normalSchedulersTransformer());
    }

    /**
     * 修改手机号
     *
     * @return
     */
    public static Observable<HttpResultEntity> editUserPhone(HashMap<String, String> map) {
        String params = configParams(map, true, true);
        return apiService.editUserPhone(params).compose(RxSchedulerUtils.normalSchedulersTransformer());
    }

    /**
     * 修改头像
     *
     * @return
     */
    public static Observable<HttpResultEntity> editUserHead(File img) {
        Map<String, String> map = new HashMap<>();
        String params = configParams(map, true, true);
        RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), img);
        // Key，business_license
        MultipartBody.Part body = MultipartBody.Part.createFormData("img1", img.getName(), requestBody);
        return apiService.editUserHead(params, body).compose(RxSchedulerUtils.normalSchedulersTransformer());
    }

    /**
     * 厂家认证
     *
     * @return
     */
    public static Observable<HttpResultEntity> applyIdentityInfo(HashMap<String, Object> map) {
        File img = ((File) map.get("business_license"));
        map.remove("business_license");
        String params = configParams(map, true, true);
        RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), img);
        // Key，business_license
        MultipartBody.Part body = MultipartBody.Part.createFormData("business_license", img.getName(), requestBody);
        return apiService.applyIdentityInfo(params, body).compose(RxSchedulerUtils.normalSchedulersTransformer());
    }

    /**
     * 收藏管理-信息
     *
     * @return
     */
    public static Observable<CollectionInfoEntity> collectionInfoList() {
        Map<String, String> map = new HashMap<>();
        String params = configParams(map, true, true);
        return apiService.collectionInfoList(params).compose(RxSchedulerUtils.normalSchedulersTransformer());
    }

    /**
     * 收藏管理-经销商
     *
     * @return
     */
    public static Observable<CollectionAgencyEntity> collectionAgencyList() {
        Map<String, String> map = new HashMap<>();
        String params = configParams(map, true, true);
        return apiService.collectionAgencyList(params).compose(RxSchedulerUtils.normalSchedulersTransformer());
    }

    /**
     * 收藏管理-商品
     *
     * @return
     */
    public static Observable<CollectionStoreEntity> collectionStoreList() {
        Map<String, String> map = new HashMap<>();
        String params = configParams(map, true, true);
        return apiService.collectionStoreList(params).compose(RxSchedulerUtils.normalSchedulersTransformer());
    }

    /**
     * 收藏管理-厂家
     *
     * @return
     */
    public static Observable<CollectionProducerEntity> collectionProducerList() {
        Map<String, String> map = new HashMap<>();
        String params = configParams(map, true, true);
        return apiService.collectionProducerList(params).compose(RxSchedulerUtils.normalSchedulersTransformer());
    }

    /**
     * 消息-询单信息
     *
     * @return
     */
    public static Observable<MessageCallEntity> messageCallInfo() {
        Map<String, String> map = new HashMap<>();
        map.put("ref_type", "1");
        String params = configParams(map, true, true);
        return apiService.messageCallInfo(params).compose(RxSchedulerUtils.normalSchedulersTransformer());
    }

    /**
     * 消息-评论
     *
     * @return
     */
    public static Observable<MessageCommentEntity> messageCommentInfo() {
        Map<String, String> map = new HashMap<>();
        map.put("ref_type", "2");
        String params = configParams(map, true, true);
        return apiService.messageCommentInfo(params).compose(RxSchedulerUtils.normalSchedulersTransformer());
    }

    /**
     * 消息-系统公告
     *
     * @return
     */
    public static Observable<MessageNoticeEntity> messageNoticeInfo() {
        Map<String, String> map = new HashMap<>();
        String params = configParams(map, true, true);
        return apiService.messageNoticeInfo(params).compose(RxSchedulerUtils.normalSchedulersTransformer());
    }

    /**
     * 创建店铺
     *
     * @return
     */
    public static Observable<ShopInfoEntity> applyShopInfo(ShopInfoRequest request, Uri uri) {
        String params = configParams(request, true, true);
        File logo = new File(FileUtils.getRealPathFromUri(Utils.getApp(), uri));
        RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), logo);
        // Key，logo
        MultipartBody.Part body = MultipartBody.Part.createFormData("logo", logo.getName(), requestBody);
        return apiService.applyShopInfo(params, body).compose(RxSchedulerUtils.normalSchedulersTransformer());
    }

    /**
     * 发布信息（供应，采购）
     *
     * @param map
     * @return
     */
    public static Observable<ApplyInfoEntity> applyInfo(ApplyInfoRequest map) {
        MultipartBody.Part[] parts = new MultipartBody.Part[map.getImgList().size()];
        for (int i = 0; i < map.getImgList().size(); i++) {
            if (map.getImgList().get(i) != null) {
                //                File file = compressImg(new File(map.getImgList().get(i)));
                File file = new File(map.getImgList().get(i));
                RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
                // Key，business_license
                MultipartBody.Part body = MultipartBody.Part.createFormData("img" + (i + 1), file.getName(), requestBody);
                parts[i] = body;
            }
        }
        String params = configParams(map, true, true);
        Observable<ApplyInfoEntity> info;
        if (map.getImgList().size() > 1) {
            info = apiService.applyInfo(params, parts);
        } else {
            info = apiService.applyInfo(params);
        }
        return info.compose(RxSchedulerUtils.normalSchedulersTransformer());
    }

    /**
     * 发布经销商
     *
     * @param map
     * @return
     */
    public static Observable<HttpResultEntity> applyAgency(ApplyAgencyRequest map) {
        String params = configParams(map, true, true);
        Observable<HttpResultEntity> info = apiService.applyAgency(params);
        return info.compose(RxSchedulerUtils.normalSchedulersTransformer());
    }

    /**
     * 发布经销商标签列表
     *
     * @return
     */
    public static Observable<AgencyTagEntity> agencyTagList() {
        Map<String, String> map = new HashMap<>();
        String params = configParams(map, true, true);
        return apiService.agencyTagList(params).compose(RxSchedulerUtils.normalSchedulersTransformer());
    }

    private static File compressImg(File imgFile) {
        Bitmap bitmap = ImageUtils.compressByQuality(BitmapFactory.decodeFile(imgFile.getPath()), 50);
        File fileCache = new File(CACHE_PATH);
        if (!fileCache.exists()) {
            fileCache.mkdirs();
        }
        File file = new File(CACHE_PATH + File.separator + System.currentTimeMillis() + ".jpg");
        try {
            if (file.exists()) {
                file.delete();
                file.createNewFile();
            } else {
                file.createNewFile();
            }
        } catch (IOException e) {
            e.printStackTrace();
            return imgFile;
        }
        boolean b = FileIOUtils.writeFileFromBytesByChannel(file, ImageUtils.bitmap2Bytes(bitmap, Bitmap.CompressFormat.JPEG), false);
        if (b) {
            return file;
        } else {
            return imgFile;
        }
    }

    /**
     * 编辑发布信息（供应，采购）
     *
     * @param map
     * @return
     */
    public static Observable<ApplyInfoEntity> updateApplyInfo(ApplyInfoRequest map) {
        List<MultipartBody.Part> parts = new ArrayList<>();
        for (int i = 0; i < map.getImgList().size(); i++) {
            if (map.getImgList().get(i) != null) {
                File file = new File(map.getImgList().get(i));
                if (file.exists()) {
                    RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
                    // Key，business_license
                    MultipartBody.Part body = MultipartBody.Part.createFormData("img" + (i + 1), file.getName(), requestBody);
                    parts.add(body);
                }
            }
        }
        String params = configParams(map, true, true);
        Observable<ApplyInfoEntity> info;
        if (parts.size() >= 1) {
            info = apiService.updateApplyInfo(params, parts.toArray(new MultipartBody.Part[parts.size()]));
        } else {
            info = apiService.updateApplyInfo(params);
        }
        return info.compose(RxSchedulerUtils.normalSchedulersTransformer());
    }

    /**
     * 会员中心
     *
     * @return
     */
    public static Observable<VipEntity> vipCenter() {
        Map<String, String> map = new HashMap<>();
        String params = configParams(map, true, true);
        return apiService.vipCenter(params).compose(RxSchedulerUtils.normalSchedulersTransformer());
    }

    /**
     * 开通会员
     *
     * @return
     */
    public static Observable<HttpResultEntity> openVip(Map<String, String> map) {
        String params = configParams(map, true, true);
        return apiService.openVip(params).compose(RxSchedulerUtils.normalSchedulersTransformer());
    }

    /**
     * 会员中心
     *
     * @return
     */
    public static Observable<HttpResultEntity> serviceInfo() {
        Map<String, String> map = new HashMap<>();
        String params = configParams(map, true, true);
        return apiService.serviceInfo(params).compose(RxSchedulerUtils.normalSchedulersTransformer());
    }

    /**
     * 发布管理
     *
     * @return
     */
    public static Observable<ApplyManagerEntity> applyManagerList() {
        Map<String, String> map = new HashMap<>();
        String params = configParams(map, true, true);
        return apiService.applyManagerList(params).compose(RxSchedulerUtils.normalSchedulersTransformer());
    }

    /**
     * 删除发布
     *
     * @return
     */
    public static Observable<HttpResultEntity> deleteApply(Map<String, String> map) {
        String params = configParams(map, true, true);
        return apiService.deleteApply(params).compose(RxSchedulerUtils.normalSchedulersTransformer());
    }

    /**
     * 用户分类下的店铺
     *
     * @return
     */
    public static Observable<SetShopEntity> userShopList(Map<String, String> map) {
        String params = configParams(map, true, true);
        return apiService.userShopList(params).compose(RxSchedulerUtils.normalSchedulersTransformer());
    }

    /**
     * 编辑原有店铺
     *
     * @return
     */
    public static Observable<EditShopEntity> editShopInfo(Map<String, String> map) {
        String params = configParams(map, true, true);
        return apiService.editShopInfo(params).compose(RxSchedulerUtils.normalSchedulersTransformer());
    }

    /**
     * 更新原有店铺信息
     *
     * @return
     */
    public static Observable<HttpResultEntity> updateShopInfo(ShopInfoRequest map) {
        String params = configParams(map, true, true);
        return apiService.updateShopInfo(params).compose(RxSchedulerUtils.normalSchedulersTransformer());
    }

    /**
     * 更新原有店铺信息
     *
     * @return
     */
    public static Observable<HttpResultEntity> updateShopInfo(ShopInfoRequest map, Uri uri) {
        File logo = new File(FileUtils.getRealPathFromUri(Utils.getApp(), uri));
        RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), logo);
        // Key，logo
        MultipartBody.Part body = MultipartBody.Part.createFormData("logo", logo.getName(), requestBody);
        String params = configParams(map, true, true);
        return apiService.updateShopInfo(params, body).compose(RxSchedulerUtils.normalSchedulersTransformer());
    }

    /**
     * 店铺的商品列表
     *
     * @param map
     * @return
     */
    public static Observable<NewListEntity> storeList(HashMap<String, Object> map) {
        String params = configParams(map, true, true);
        return apiService.storeList(params).compose(RxSchedulerUtils.normalSchedulersTransformer());
    }

    /**
     * 删除商品
     *
     * @return
     */
    public static Observable<HttpResultEntity> deleteStore(Map<String, String> map) {
        String params = configParams(map, true, true);
        return apiService.deleteStore(params).compose(RxSchedulerUtils.normalSchedulersTransformer());
    }

    /**
     * 更新商品
     *
     * @param map
     * @return
     */
    public static Observable<HttpResultEntity> updateStoreInfo(ApplyStoreRequest map) {
        ArrayList<MultipartBody.Part> parts = new ArrayList<>();
        for (int i = 0; i < map.getImgList().size(); i++) {
            if (map.getImgList().get(i) != null) {
                File file = new File(map.getImgList().get(i));
                if (file.exists()) {
                    RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
                    // Key，business_license
                    MultipartBody.Part body = MultipartBody.Part.createFormData("img" + (i + 1), file.getName(), requestBody);
                    parts.add(body);
                }
            }
        }
        String params = configParams(map, true, true);
        Observable<HttpResultEntity> info;
        if (parts.size() > 0) {
            info = apiService.updateStoreInfo(params, parts.toArray(new MultipartBody.Part[parts.size()]));
        } else {
            info = apiService.updateStoreInfo(params);
        }
        return info.compose(RxSchedulerUtils.normalSchedulersTransformer());
    }

    /**
     * 搜索
     *
     * @return
     */
    public static Observable<SearchEntity> searchInfo(Map<String, String> map) {
        String params = configParams(map, true, true);
        return apiService.searchInfo(params).compose(RxSchedulerUtils.normalSchedulersTransformer());
    }

    /**
     * 分享内容链接
     *
     * @return
     */
    public static Observable<ShareEntity> shareContent(HashMap<String, String> map) {
        String params = configParams(map, true, true);
        return apiService.shareContent(params).compose(RxSchedulerUtils.normalSchedulersTransformer());
    }

    /**
     * 修改密码
     *
     * @return
     */
    public static Observable<HttpResultEntity> changePwd(HashMap<String, String> map) {
        String params = configParams(map, true, true);
        return apiService.changePwd(params).compose(RxSchedulerUtils.normalSchedulersTransformer());
    }

    /**
     * 参数设置
     *
     * @param obj
     * @return
     */
    private static String configParams(Object obj, boolean isLogin, boolean isUserId) {
        String encode = "";
        try {
            String jsonString = JsonUtils.createJsonString(obj);
            Map<String, String> mapStr = JsonUtils.getMapStr(jsonString);
            mapStr.put("anycode", isLogin ? httpConfig.getLoginToken() : httpConfig.getAccessToken());
            mapStr.put("from_type", "1");
            mapStr.put("device_model", httpConfig.getDeviceModel());
            mapStr.put("device_number", httpConfig.getDeviceId());
            mapStr.put("user_id", isUserId ? httpConfig.getUserId() : "-1");
            mapStr.put("expire_time", FormatUtils.formatHttpDate(new Date()));

            String json = JsonUtils.createJsonString(mapStr);
            Logger.v("OkHttp---" + json);
            encode = DES3.encode(json, DES3.secretKey);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return encode;
    }

    /**
     * sort by a-z
     */
    public static class CityComparator implements Comparator<City> {
        @Override
        public int compare(City lhs, City rhs) {
            String a = lhs.getPinyin().substring(0, 1);
            String b = rhs.getPinyin().substring(0, 1);
            return a.compareTo(b);
        }
    }
}
